package com.example.celldian.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBArmor extends SQLiteOpenHelper {

	public DBArmor(Context activity) {
		super(activity, "Celldian.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String query = "CREATE TABLE armors ( itemname TEXT PRIMARY KEY, armor TEXT, strength TEXT, " +
					   "intelect TEXT, agility TEXT, stamina TEXT, part TEXT, type TEXT, " + 
					   "class TEXT, bonusname TEXT)";
		
		db.execSQL(query);	
		
		Log.i("DATABASE DBArmor:","TABLE armors SUCCESSFULY CREATED!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String query = "DROP TABLE IF EXISTS armors";
		
		db.execSQL(query);
		onCreate(db);				
		
		Log.i("DATABASE DBArmor:","UPDATE SUCCESSFUL!");
	}
	
	public void insertArmor(HashMap<String ,String> armorValues) {
			
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("itemname", armorValues.get("itemname"));
		values.put("armor", armorValues.get("armor"));
		values.put("strength", armorValues.get("strength"));
		values.put("intelect", armorValues.get("intelect"));
		values.put("agility", armorValues.get("agility"));
		values.put("stamina", armorValues.get("stamina"));
		values.put("part", armorValues.get("part"));
		values.put("type", armorValues.get("type"));
		values.put("class", armorValues.get("class"));
		values.put("bonusname", armorValues.get("bonusname"));
		
		db.insert("armors", null, values);
		db.close();
		
		Log.i("DATABASE DBArmor:","WEAPON INSERT WAS SUCCESSFUL!");
	}
	
	public int updateArmor(HashMap<String ,String> armorValues) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("itemname", armorValues.get("itemname"));
		values.put("armor", armorValues.get("armor"));
		values.put("strength", armorValues.get("strength"));
		values.put("intelect", armorValues.get("intelect"));
		values.put("agility", armorValues.get("agility"));
		values.put("stamina", armorValues.get("stamina"));
		values.put("part", armorValues.get("part"));
		values.put("type", armorValues.get("type"));
		values.put("class", armorValues.get("class"));
		values.put("bonusname", armorValues.get("bonusname"));
		
		Log.i("DATABASE DBArmor:","ARMOR UPDATE WAS SUCCESSFUL!");
		
		return db.update("armors", values, "itemname" + "?=", new String[] {armorValues.get("itemname")});
	}
	
	public void deleteArmor(String itemname) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		String query = "DELETE FROM armors WHERE itemname='" + itemname + "'";
		
		db.execSQL(query);
		
		Log.i("DATABASE DBArmor:","ARMOR DELETE WAS SUCCESSFUL!");
	}
	
	public ArrayList<HashMap<String, String>> getArmors() {
		
		ArrayList<HashMap<String, String>> armorList = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM armors";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				HashMap<String, String> armors = new HashMap<String, String>();
				
				armors.put("itemname", cursor.getColumnName(0));
				armors.put("armor", cursor.getColumnName(1));
				armors.put("strength", cursor.getColumnName(2));
				armors.put("intelect", cursor.getColumnName(3));
				armors.put("agility", cursor.getColumnName(4));
				armors.put("stamina", cursor.getColumnName(5));
				armors.put("part", cursor.getColumnName(6));
				armors.put("type", cursor.getColumnName(7));
				armors.put("class", cursor.getColumnName(8));
				armors.put("bonusname", cursor.getColumnName(9));
				
				armorList.add(armors);
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBArmor:","ARMORS RETURN WAS SUCCESSFUL!");
		
		return armorList;
	}
	
	public HashMap<String, String> getArmor(String itemname){
		
		HashMap<String, String> armors = new HashMap<String, String>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM armors WHERE itemname='" + itemname + "'";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				armors.put("itemname", cursor.getColumnName(0));
				armors.put("armor", cursor.getColumnName(1));
				armors.put("strength", cursor.getColumnName(2));
				armors.put("intelect", cursor.getColumnName(3));
				armors.put("agility", cursor.getColumnName(4));
				armors.put("stamina", cursor.getColumnName(5));
				armors.put("part", cursor.getColumnName(6));
				armors.put("type", cursor.getColumnName(7));
				armors.put("class", cursor.getColumnName(8));
				armors.put("bonusname", cursor.getColumnName(9));
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBArmor:","ARMOR RETURN WAS SUCCESSFUL!");
		
		return armors;
	}
}
